package solution;

import com.alibaba.fastjson2.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class mianshi {

    public static void main(String[] args) {
        //1
        int num1[] = {7,1,5,3,6,4};
        int a = mianshi.Solution_1.twoSum(num1);
        int b = mianshi.Solution_1.two(num1);


        System.out.println("1:"+ JSONObject.toJSONString(a));
        System.out.println("2:"+ JSONObject.toJSONString(b));

    }

    /**
     * 20240630 字节跳动
     */
    static class Solution_1 {
        public static int twoSum(int[] nums) {
            int temp = nums[0];
            int max = 0;
            for (int i=1;i<=nums.length-1;i++){
                if (temp>nums[i]){
                    temp = nums[i];
                }
                max = Math.max(max,nums[i]-temp);
            }
            return max;
        }

        /**
         * 上涨则买，下降就卖
         * @param nums
         * @return
         */
        public static int two(int[] prices) {
            int temp = prices[0];
            int sum = 0;
            for (int i= 1 ; i<prices.length;i++){
                //判断第i天是否上涨，上涨则不操作
                if (prices[i-1] <= prices[i]){
                    //循环结束一定要卖出股票获得利润
                    if (i == prices.length-1){
                        sum = sum + (prices[i]-temp);
                    }
                    continue;
                }
                // 否则卖出股票，并获得利润
                sum = sum + (prices[i-1]-temp);
                temp = prices[i];
            }
            return sum;

        }

    }
}
